var express = require('express');
var router = express.Router();


var userDao = require('../dao/userDao');
var response = require('../dao/response');

// Get users
router.get('/userInfo', (req, res) => {

    tokenVerifiy(req, res);
    console.log(req.user);

    res.json({ success: true, data: req.user });
});



function tokenVerifiy(req, res) {
    if (!req.user) return res.json({ success: false, error: 'Bad Token', msg: '用户过期' });

}


// sign up
router.get('/sign', (req, res) => {


    var username = req.query.username;

    var password = req.query.password;
    var user = {
        username: username,
        password: password
    };

    console.log(user);
    // res.json({success:true});

    if (!username || !password) {
        response.error(res, 'error', 'user is null');
    } else {


        userDao.addUser(user, res);
    }

});

// sign in
router.get('/login', (req, res) => {

    var username = req.query.username;

    var password = req.query.password;

    var user = {
        username: username,
        password: password
    };

    console.log(user);
    // res.json({success:true});

    if (!username || !password) {
        response.error(res, 'error', 'user is null');
    } else {

        userDao.login(user, res);
    }

});

// sign in
router.post('/login', (req, res) => {


    var username = req.body.username;

    var password = req.body.password;

    var user = {
        username: username,
        password: password
    };

    console.log(user);
    // res.json({success:true});

    if (!username || !password) {
        response.error(res, 'error', 'user is null');
    } else {

        userDao.login(user, res);
    }

});


// 
router.post('/addFriendRequest', (req, res) => {

    var _name = req.body.searchName;

    userDao.addFriendRequest(_name, res);


});


module.exports = router;